//
//  TZShowController.m
//  BSBDJ
//
//  Created by user on 16/5/25.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZShowController.h"
#import "TZShowCell.h"
#import "TZEssenceItem.h"
#import "TZBaseItem.h"
#import "TZInfoItem.h"
#import "TZDetailVc.h"
#import "UMSocial.h"
#import "UMSocialData.h"
#import "UMSocialSnsService.h"
#import "ShareTool.h"
@interface TZShowController ()<UMSocialUIDelegate>
/** 精华数组*/
@property (nonatomic ,strong) NSMutableArray *esseneList;
/** page*/
@property (nonatomic, assign) int page;

/** 用来加载下一页数据的参数 */
@property (nonatomic, copy) NSString *maxtime;
@end
 
@implementation TZShowController
static NSString *ID = @"showcell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    //>通知
    [self setupNote];
    [self setupUI];
    [self setupRefresh];
    self.page = 1;
}

- (void)setupNote
{
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabbarBtnClick) name:tabbarBtnClickNotificationCenter object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toolBarBtnClick) name:toolBarBtnClickNotificationCenter object:nil];
}


- (void)tabbarBtnClick{
    if (self.tableView.scrollsToTop == NO) return;
    if (self.tableView.window == nil) return;
    [self.tableView.mj_header beginRefreshing];
}
- (void)toolBarBtnClick{
    [self tabbarBtnClick];
}
// 设置刷新
- (void)setupRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(starHeaderRefresh)];
    
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(starFooterRefresh)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)starHeaderRefresh{
    [self sendRequest];
}

- (void)starFooterRefresh{
   
    [self sendMore];
}

// 设置UI
- (void)setupUI{
    [self initTableView];
}
// 初始化TableView
- (void)initTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"TZShowCell" bundle:nil] forCellReuseIdentifier:ID];
    self.tableView.backgroundColor = XMGColor(225, 225, 225);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
}


// 请求新数据
- (void)sendRequest{
    MJWeakSelf
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"data";
    parameters[@"type"] = @(weakSelf.type);
    [HttpRequestList requestEssenceList:requestPath paramaters:parameters success:^(TZBaseItem *baseItem) {
        if (baseItem.list.count) {
            weakSelf.esseneList = (NSMutableArray *)baseItem.list;
            weakSelf.maxtime = baseItem.info.maxtime;
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];

    }];
}
// 请求更多
- (void)sendMore{
    
    MJWeakSelf
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"data";
    parameters[@"type"] = @(weakSelf.type);
    parameters[@"maxtime"] = self.maxtime;

    [HttpRequestList requestEssenceList:requestPath paramaters:parameters success:^(TZBaseItem *baseItem) {
        if (baseItem.list.count) {
            [weakSelf.esseneList addObjectsFromArray:baseItem.list];
            weakSelf.maxtime = baseItem.info.maxtime;
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_footer endRefreshing];
        }else{
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } failure:^(NSError *error) {
        
    }];
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"dealloc---showController");
}




#pragma mark --------------
#pragma mark 数据源TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.esseneList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TZShowCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.item = self.esseneList[indexPath.row];
    cell.block = ^(TZEssenceItem *item){
        
        //分享
        
          UIImage *originalImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:item.small_image];
        [UMSocialData defaultData].extConfig.title = @"分享百思不得姐";
        [UMSocialData defaultData].extConfig.qqData.url = item.weixin_url;
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"5746fa3667e58e1914002df5"
                                          shareText:[NSString stringWithFormat:@"%@,%@",item.text,item.weixin_url]
                                         shareImage:originalImage
                                    shareToSnsNames:nil
                                           delegate:self];
        
    };
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TZEssenceItem *item = self.esseneList[indexPath.row];

    return item.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    TZShowCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    TZEssenceItem *item = self.esseneList[indexPath.row];
    cell.item = item;
    TZDetailVc *detailVc = [TZDetailVc new];
    detailVc.data_id = item.ID;
    [self.navigationController pushViewController:detailVc animated:YES];
}

#pragma mark --------------
#pragma mark 分享代理
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
